﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;

namespace Booking_Hotel
{
    public partial class Supplier_LoaiPhong_DichVu : System.Web.UI.Page
    {
        ThanhBHotelDataContext tbk = new ThanhBHotelDataContext();
        Supplier_Hotel_LoaiPhong_DichVu entities = new Supplier_Hotel_LoaiPhong_DichVu();
        int DichVuID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                ShowDichVu();
            }
        }

        protected void ShowDichVu()
        {
            var dichvu = from dv in tbk.Supplier_Hotels
                        join shl in tbk.Supplier_Hotel_LoaiPhongs on dv.Supplier_HotelID equals shl.Supplier_HotelID
                        join shd in tbk.Supplier_Hotel_LoaiPhong_DichVus on shl.Supplier_Hotel_LoaiPhongID equals shd.Supplier_Hotel_LoaiPhongID
                        where dv.SupplierID == (int)Session["ss_SupplierID"]
                        select new
                        {
                            Supplier_Hotel_LoaiPhong_DichVuID = shd.Supplier_Hotel_LoaiPhong_DichVuID,
                            Supplier_Hotel_LoaiPhongID = shd.Supplier_Hotel_LoaiPhongID,
                            TenLoaiPhong = shl.TenLoaiPhong + "(" + dv.TenKhachSan + ")",
                            TenDichVu = shd.TenDichVu,
                            Description = shd.Description
                        };
            GridView1.DataSource = dichvu;
            GridView1.DataBind();

        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_Hotel_LoaiPhong_DichVuID"] = int.Parse(select.Cells[0].Text.Trim());
            DDL_TenLoaiPhong.SelectedValue = select.Cells[1].Text.Trim();
            txt_TenDichVu.Text = select.Cells[3].Text.Trim();
            txt_diengiai.Text = select.Cells[4].Text.Trim();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton bt_Delete = sender as ImageButton;
            GridViewRow row = (GridViewRow)bt_Delete.NamingContainer;
            DichVuID = int.Parse(row.Cells[0].Text.Trim());
            Supplier_Hotel_LoaiPhong_DichVu shld = tbk.Supplier_Hotel_LoaiPhong_DichVus.Where(c => c.Supplier_Hotel_LoaiPhong_DichVuID == DichVuID).Single();
            tbk.Supplier_Hotel_LoaiPhong_DichVus.DeleteOnSubmit(shld);
            tbk.SubmitChanges();
            ShowDichVu();
        }

        protected void bt_insert_Click(object sender, EventArgs e)
        {
            try
            {
                entities = new Supplier_Hotel_LoaiPhong_DichVu();
                entities.Supplier_Hotel_LoaiPhongID =int.Parse(DDL_TenLoaiPhong.SelectedValue);
                entities.TenDichVu = txt_TenDichVu.Text.Trim();
                entities.Description = txt_diengiai.Text.Trim();
                Supplier_LoaiPhong_DichVuDAO sld = new Supplier_LoaiPhong_DichVuDAO();
                sld.addSupplier_Hotel_LoaiPhong_DichVu(entities);
                ShowDichVu();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void bt_update_Click(object sender, EventArgs e)
        {
            try
            {
                int check_Supplier_Hotel_LoaiPhong_DichVuID = (int)(Session["ss_Supplier_Hotel_LoaiPhong_DichVuID"]);
                if (check_Supplier_Hotel_LoaiPhong_DichVuID != null)
                {
                    entities = new Supplier_Hotel_LoaiPhong_DichVu();
                    entities.Supplier_Hotel_LoaiPhong_DichVuID = check_Supplier_Hotel_LoaiPhong_DichVuID;
                    entities.Supplier_Hotel_LoaiPhongID = int.Parse(DDL_TenLoaiPhong.SelectedValue);
                    entities.TenDichVu = txt_TenDichVu.Text.Trim();
                    entities.Description = txt_diengiai.Text.Trim();
                    Supplier_LoaiPhong_DichVuDAO sd = new Supplier_LoaiPhong_DichVuDAO();
                    sd.updateSupplier_Hotel_LoaiPhong_DichVu(entities);
                    ShowDichVu();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}